package com.qidian.server.socket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.log4j.Logger;

import com.qidian.common.Constants;


public class AnswerServer extends Thread{
	protected String 		serverIP  ;
    protected int       	serverPort ;
    protected int 			num_threads = Constants.num_threads_get_answer ;
    DatagramSocket  		server = null ;
    protected boolean      isStopped    = false;
    protected Thread       runningThread= null;
    protected ExecutorService threadPool = Executors.newFixedThreadPool(num_threads);
   
    static Logger logger = Logger.getLogger(AnswerServer.class) ;
    
//    public AnswerServer(){
//    } 
    public AnswerServer(String ip ,int port){
    	this.serverIP = ip ;
        this.serverPort = port;
    }

    public int getPort()
    {
    	return serverPort ;
    }
    
    public void run(){
        synchronized(this){
            this.runningThread = Thread.currentThread();
        }
        
        try 
        {
			openServerSocket();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			logger.error(e1.getMessage()) ;
			return ;
		}
        while(! isStopped()){
            
        	byte[] recvBuf = new byte[128];
            DatagramPacket recvPacket = new DatagramPacket(recvBuf , recvBuf.length);
            try 
            {
                server.receive(recvPacket);
            } catch (IOException e) {
                if(isStopped()) {
                    System.out.println("Server Stopped.") ;
                    return;
                }
                logger.error(e.getMessage()) ;
            }
            this.threadPool.execute(new AnswerWorker(server,recvPacket,"Thread Pooled Server"));

        }
        this.threadPool.shutdown();
        System.out.println("Server Stopped.") ;
    }


    private synchronized boolean isStopped() {
        return this.isStopped;
    }

    public synchronized void stopServer(){
        isStopped = true;
        server.close() ;
    }

    private void openServerSocket() throws IOException {
    	
    	System.out.println("Answer start up "+this.serverIP+":"+this.serverPort);
    	InetAddress ip = InetAddress.getByName(this.serverIP) ;
        server = new DatagramSocket(this.serverPort,ip);
        server.setReceiveBufferSize(1024*1024); 
        server.setSendBufferSize(1024*1024) ;
        server.setBroadcast(false) ;
        System.out.println("Receive buffer size:"+server.getReceiveBufferSize()) ;
        System.out.println("Send buffer size:"+server.getSendBufferSize()) ;
    }
    
    public static void main(String[] args) throws InterruptedException, SocketException
    {
//    	AnswerServer server = new AnswerServer() ;
////    	server.run() ;
//    	server.start() ;
//    	System.out.println(123);
//    	Thread.sleep(30*1000);
//    	System.out.println(456);
//    	server.stopServer() ;
//    	System.out.println(789);
        DatagramSocket server = new DatagramSocket();
        System.out.println(server.getReceiveBufferSize()) ;
    	return ;
    }
   
}